{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "liberal-absorption",
   "metadata": {},
   "source": [
    "# Abgespeckte Version des Clusterings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "alone-international",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import plotly.express as px\n",
    "import plotly.graph_objects as go\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "excessive-grass",
   "metadata": {},
   "source": [
    "## Preparing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "valid-steal",
   "metadata": {},
   "outputs": [],
   "source": [
    "clusters = pd.read_csv(\"clustering.csv\")\n",
    "european_union = json.load(open(\"european-union-countries.geojson\", \"r\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "taken-monte",
   "metadata": {},
   "outputs": [],
   "source": [
    "state_id_map = {}\n",
    "for feature in european_union[\"features\"]:\n",
    "    feature[\"id\"] = feature[\"properties\"][\"gu_a3\"]\n",
    "    state_id_map[feature[\"properties\"][\"name_long\"]] = feature[\"id\"]\n",
    "    \n",
    "clusters[\"id\"] = clusters[\"Country\"].apply(lambda x: state_id_map[x])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fourth-invention",
   "metadata": {},
   "outputs": [],
   "source": [
    "clusters.to_csv(\"final_clusters.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exterior-volume",
   "metadata": {},
   "source": [
    "## Food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "unexpected-empire",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = px.choropleth_mapbox(\n",
    "    clusters,\n",
    "    locations=\"id\",\n",
    "    geojson=european_union,\n",
    "    color=\"cluster_food\",\n",
    "    hover_name=\"Country\",\n",
    "    #hover_data=[\"Life Expectancy\"],\n",
    "    title=\"clusters by food\",\n",
    "    mapbox_style=\"carto-positron\", \n",
    "    center={\"lat\": 56.5, \"lon\": 11},\n",
    "    zoom=2.5,\n",
    "    opacity=0.7, \n",
    "    height=600,\n",
    "    width=550\n",
    ")\n",
    "fig.update_layout(margin={\"r\":0,\"t\":0,\"l\":0,\"b\":0})\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "devoted-enzyme",
   "metadata": {},
   "source": [
    "## Overall (Food+Health)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accredited-runner",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = px.choropleth_mapbox(\n",
    "    clusters,\n",
    "    locations=\"id\",\n",
    "    geojson=european_union,\n",
    "    color=\"cluster_total\",\n",
    "    hover_name=\"Country\",\n",
    "    hover_data=[\"Life Expectancy\"],\n",
    "    title=\"Clusters by health\",\n",
    "    mapbox_style=\"carto-positron\", \n",
    "    center={\"lat\": 56.5, \"lon\": 11},\n",
    "    zoom=2.5,\n",
    "    opacity=0.7, \n",
    "    height=600,\n",
    "    width=550\n",
    ")\n",
    "fig.update_layout(margin={\"r\":0,\"t\":0,\"l\":0,\"b\":0})\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "entire-preserve",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "clusters\n",
    "path = 'https://raw.githubusercontent.com/FranzMichaelFrank/health_eu/main/'\n",
    "\n",
    "def cz(s):\n",
    "    if s == \"Czech Republic\":\n",
    "        return \"Czechia\"\n",
    "    else:\n",
    "        return s\n",
    "    \n",
    "clusters[\"Country\"] = clusters[\"Country\"].apply(cz)\n",
    "df_scatter = pd.read_csv(path + 'scatter_data.csv', dtype={\"id\": str})\n",
    "box_cluster = df_scatter.merge(clusters[[\"Country\",\"cluster_food\",\"cluster_health\",\"cluster_total\"]], on=\"Country\")\n",
    "box_cluster = box_cluster.rename(columns={\"cluster_food\":\"Food\", \"cluster_health\":\"Health\", \"cluster_total\":\"Food & Health\"})\n",
    "box_cluster.to_csv(\"box_cluster.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "billion-medication",
   "metadata": {},
   "outputs": [],
   "source": [
    "clusts = [\"Food\", \"Health\", \"Food & Health\"]\n",
    "\n",
    "ots = [\"Alcoholic Beverages\", \"Animal fats\", \"Cereals - Excluding Beer\", \"Eggs\", \n",
    "                \"Fish, Seafood\", \"Fruits - Excluding Wine\", \"Meat\", \"Milk - Excluding Butter\",\n",
    "                \"Offals\", \"Oilcrops\", \"Pulses\", \"Spices\", \"Starchy Roots\",\"Stimulants\", \n",
    "                \"Sugar & Sweeteners\", \"Treenuts\", \"Vegetable Oils\",\n",
    "                \"Vegetables\",\"Obesity\", \"Diabetes Prevalence\", \"Cardiovascular Death Rate\", \"Life Expectancy\", \"Health Expenditure\" ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cardiac-variation",
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.express as px\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "innovative-output",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "annoying-resistance",
   "metadata": {},
   "outputs": [],
   "source": [
    "li = [, '#46039f', , '#9c179e', , '#d8576b', , '#fb9f3a', , '#f0f921'].reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "vocational-clear",
   "metadata": {},
   "outputs": [],
   "source": [
    "li"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "twenty-precipitation",
   "metadata": {},
   "outputs": [],
   "source": [
    "['#fdca26', '#ed7953', '#bd3786', '#7201a8', '#0d0887']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "opposite-vertical",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "military-addiction",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "floral-congress",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "paperback-broadcast",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "variable-policy",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
